package com.example.front.controller;

import com.example.api.model.Product;
import com.example.api.pojo.MultiProduct;
import com.example.common.util.CommonUtil;
import com.example.front.view.PageInfo;
import com.example.front.view.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description：
 * @Author：曹杰克
 * @CreateTime：2024/7/30 14:40
 */
@Api("理财产品功能")
@RestController
@RequestMapping("/v1")
public class ProductInfoController extends BaseController{

    @ApiOperation(value = "首页三类产品列表",notes = "一个新手宝，三个优选，三个散标")
    @RequestMapping("/product/index")
    public RespResult queryProductIndex(){
        MultiProduct multiProduct = productService.queryIndexPageProduct();
        return new RespResult(200,"ok",multiProduct);
    }

    @ApiOperation(value = "按产品类型分页查询",notes = "")
    @RequestMapping("/product/list")
    public RespResult queryProductByType(@RequestParam("pType") Integer pType,@RequestParam(value = "pageNo",required = false,defaultValue = "1")  Integer pageNo,@RequestParam(value = "pageSize",required = false,defaultValue = "9") Integer pageSize){
        RespResult result = RespResult.fail();
        if(pType == 0 ||pType == 1 ||pType == 2){
            pageNo = CommonUtil.defaultPageNo(pageNo);
            pageSize = CommonUtil.defaultPageSize(pageSize);
            Integer offset = (pageNo-1)*pageSize;
            //分页处理，记录总数
            Integer recordNums = productService.queryRecordNumsByType(pType);
            if(recordNums > 0){
                //产品集合
                List<Product>  products = productService.queryByTypeLimit(pType, offset, pageSize);
                result = RespResult.success();
                result.setList(products);
                PageInfo page = new PageInfo(pageNo, pageSize, recordNums);
                result.setPage(page);
            }

        }
        return result;
    }
}
